package com.personal.system.utils;

import com.personal.model.system.SysDept;

import java.util.ArrayList;
import java.util.List;

public class DeptHelper {

    //构建树形结构
    public static List<SysDept> bulidTree(List<SysDept> sysMenuList) {
        //创建集合封装最终数据
        List<SysDept> trees = new ArrayList<>();
        //遍历所有菜单集合
        for (SysDept sysDept : sysMenuList) {
            //找到递归入口，parentId=0
            if (sysDept.getParentId().longValue() == 0) {
                trees.add(findChildren(sysDept, sysMenuList));
            }
        }
        return trees;
    }

    //从根节点进行递归查询，查询子节点
    // 判断 id =  parentid是否相同，如果相同是子节点，进行数据封装
    private static SysDept findChildren(SysDept SysDept, List<SysDept> treeNodes) {
        //数据初始化
        SysDept.setChildren(new ArrayList<SysDept>());
        //遍历递归查找
        for (SysDept it : treeNodes) {
            //获取当前菜单id
//            String id = SysDept.getId();
//            long cid = Long.parseLong(id);
            //获取所有菜单parentid
//            Long parentId = it.getParentId();
            //比对
            if (Long.parseLong(SysDept.getId()) == it.getParentId()) {
                if (SysDept.getChildren() == null) {
                    SysDept.setChildren(new ArrayList<>());
                }
                SysDept.getChildren().add(findChildren(it, treeNodes));
            }
        }
        return SysDept;
    }
}
